import { createSlice } from "@reduxjs/toolkit";
import { fetchMenuList } from "@/apis/menu";

const menusSlice = createSlice({
  name: "menus",
  initialState: {
    items: [],
    loading: false,
    error: null,
  },
  reducers: {
    fetchMenusStart(state) {
      state.loading = true;
      state.error = null;
    },
    fetchMenusSuccess(state, action) {
      state.items = action.payload;
      state.loading = false;
    },
    fetchMenusFailure(state, action) {
      state.error = action.payload;
      state.loading = false;
    },
  },
});

export const { fetchMenusStart, fetchMenusSuccess, fetchMenusFailure } =
  menusSlice.actions;
//获取后端路由菜单
export const fetchMenuItems = () => async (dispatch) => {
  dispatch(fetchMenusStart());
  const res = await fetchMenuList();
  dispatch(fetchMenusSuccess(res.data));
  // console.log("后端路由菜单", res.data);
};
export default menusSlice.reducer;
